<?php
namespace app\index\controller;
use stock\other\PageSql;
use \think\Request;
use think\Db; 
class Category extends \app\index\controller\Common
{
	public function cate_pwd_deal(){
		$request = request()->param();
		$opwd=$request['opwd'];
		$pwd1=$request['pwd1'];
		$pwd=$request['pwd'];
		if(empty($opwd)||empty($pwd)||empty($pwd1)){
			$this->error('请输入密码');
		}
		if($pwd!=$pwd1){
			$this->error('两次密码不一致');
		}
		$data['pwd']=md5(addslashes($pwd));
		$sql="id ='".$_SESSION['admin_id']."' and pwd='".md5(addslashes($opwd))."' and status=0";
		$goodsObj=Db::name('category');
		$re=$goodsObj->where($sql)->update($data);
		if($re){
			$this->success('密码修改成功', '/');
		}else{
			$this->error('修改，请重试');
		}	
	}
	public function cate_check(){
		$request = request()->param();
		if(empty($request['uname'])&&empty($request['name'])){
			return;
		}
		$where='id>0';
		if($request['id']){
			$id=(int)$request['id'];
			$where .=" and id !=".$id;
		}
		if($request['name']){
			$where .=" and name ='".addslashes(trim($request['name']))."'";
		}
		if($request['uname']){
			$where .=" and username ='".addslashes(trim($request['uname']))."'";
		}
		$data=Db::name('category')->where($where)->find();
		if($data)
			exit(json_encode(array('status'=>1)));
		else
			exit(json_encode(array('status'=>0)));		
	}		
	public function pwd_edit(){
		return $this->fetch();
	}
	public function cate_add(){
		$request = request()->param();
		$data=array();
		$where['status']=0;
		if(isset($request['id'])){
			$id=(int)$request['id'];
			if($id){
				$where['id']=$id;
				$goodsObj=Db::name('category');
				$data=$goodsObj->where($where)->find();				
			}
		}

		$data1=Db::name('category')->where('id=1')->find();	
		$this->assign('data1',$data1);					
		$this->assign('data',$data);	
		return $this->fetch();
	}
	
	public function cate_deal_add(){
		$request = request()->param();
	    $data['name']=addslashes($request['name']);
	    $data['parent_id']=(int)$request['parent'];
	    $data['username']=addslashes($request['username']);
	    $message_suc='门店添加成功';
	    $message_fai='门店重复';
	    if(!empty($data['name'])){
		    $id='';
		    if(isset($request['id'])){
		    	$id=(int)$request['id'];
		    }	    
			
			$goodsObj=Db::name('category');
			if($id){
				//unset($data['pwd']);
				if($request['pwd']){
					$data['pwd']=md5(addslashes($request['pwd']));
				}
				unset($data['username']);
				$re=$goodsObj->where("id",$id)->update($data);
				$message_suc='门店修改成功';
				$this->success($message_suc, '/Index/category/cate_list');
			}else{
				$data['pwd']=md5(addslashes($request['pwd']));
				$re=$goodsObj->insert($data);
			}//echo Db::name('category')->getLastSql();exit('nn');
			if($re){
				$this->success($message_suc, '/Index/category/cate_list');
			}else{
				$this->error($message_fai);
			}	
	    }
	    $this->error('请输入门店');
	}
	public function goods_de(){
		$request = request()->param();
		$id=(int)$request['id'];
		if($id){
			$goodsObj=Db::name('goods');
			$data['status']=1;
			$goodsObj->where("id",$id)->update($data);
			$this->success('商品已删除', '/Index/goods/goods_list');			
		}else{
			$this->error('删除失败,请重试');
		}
	}
	public function cate_list(){
		$goodsObj=Db::name('category');
		$sql='id>0';
		/*
		$request = request()->param();
		
		if(isset($request['name'])){
			$sql .=" and name like '%".addslashes(trim($request['name']))."'";
		}
		*/
		$list=$goodsObj->where($sql)->order("id desc")->paginate(66);
        $page = $list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign('page', $page); 		
		return $this->fetch();
	}	
}	